package com.neusoft.neuiotms.dcam.config;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.business.ActBusiness;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.fixed.model.Asset;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetRepository;
import com.neusoft.neuiotms.dcam.workflow.model.BaofeiItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.BaofeiItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.DiaoboItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.DiaoboItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.GenghuanItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.GuihuanItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.JieyongConfrimSL;
import com.neusoft.neuiotms.dcam.workflow.model.LingyongConfrimKJ;
import com.neusoft.neuiotms.dcam.workflow.model.LingyongConfrimSL;
import com.neusoft.neuiotms.dcam.workflow.model.PeifaItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.TuikuItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.TuikuItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.WeixiuItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.WeixiuItemSL;
import com.neusoft.neuiotms.dcam.workflow.model.ZhuanyiItemKJ;
import com.neusoft.neuiotms.dcam.workflow.model.ZhuanyiItemSL;
import com.neusoft.neuiotms.dcam.workflow.repository.BaofeiItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.BaofeiItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.DiaoboItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.DiaoboItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.GenghuanItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.GuihuanItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.JieyongConfrimSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.LingyongConfrimKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.LingyongConfrimSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.PeifaItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.TuikuItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.TuikuItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.WeixiuItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.WeixiuItemSLRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.ZhuanyiItemKJRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.ZhuanyiItemSLRepository;

@Service("DcamActBusiness")
public class ActBusinessService implements ActBusiness {

    @Autowired
    private BaofeiItemKJRepository baofeiItemKJRepository;
    @Autowired
    private DiaoboItemSLRepository diaoboItemSLRepository;
    @Autowired
    private GuihuanItemSLRepository guihuanItemSLRepository;
    @Autowired
    private JieyongConfrimSLRepository jieyongConfrimSLRepository;
    @Autowired
    private LingyongConfrimKJRepository lingyongConfrimKJRepository;
    @Autowired
    private LingyongConfrimSLRepository lingyongConfrimSLRepository;
    @Autowired
    private PeifaItemSLRepository peifaItemSLRepository;
    @Autowired
    private TuikuItemKJRepository tuikuItemKJRepository;
    @Autowired
    private TuikuItemSLRepository tuikuItemSLRepository;
    @Autowired
    private WeixiuItemKJRepository weixiuItemKJRepository;
    @Autowired
    private WeixiuItemSLRepository weixiuItemSLRepository;
    @Autowired
    private DiaoboItemKJRepository diaoboItemKJRepository;
    @Autowired
    private ZhuanyiItemKJRepository zhuanyiItemKJRepository;
    @Autowired
    private BaofeiItemSLRepository baofeiItemSLRepository;
    @Autowired
    private AssetRepository assetRepository;
    @Autowired
    private GenghuanItemKJRepository genghuanItemKJRepository;
    @Autowired
    private ZhuanyiItemSLRepository zhuanyiItemSLRepository;

    @Override
    public void deleteBusinessData(final String formKey, final String workNum) {
        // TODO Auto-generated method stub

    }

    @Transactional
    @Override
    public void revokeBusinessData(final String type, final String workNum) {
        final List<Asset> list = new ArrayList<>();
        switch (type) {
        case DCAMConsts.PROCESSS_TUIKU_KJ_KEY:
            final List<TuikuItemKJ> tuikuItem = this.tuikuItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != tuikuItem) {
                tuikuItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_BAOFEI_KJ_KEY:
            final List<BaofeiItemKJ> baofeiItem = this.baofeiItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != baofeiItem) {
                baofeiItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_WEIXIU_KJ_KEY:
            final List<WeixiuItemKJ> weixiuItem = this.weixiuItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != weixiuItem) {
                weixiuItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_DIAOBO_KJ_KEY:
            final List<DiaoboItemKJ> diaoboItem = this.diaoboItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != diaoboItem) {
                diaoboItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_ZHUANYI_KJ_KEY:
            final List<ZhuanyiItemKJ> zhuanyiItem = this.zhuanyiItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != zhuanyiItem) {
                zhuanyiItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_LINGYONG_KJ_KEY:
            final List<LingyongConfrimKJ> lingyongItem = this.lingyongConfrimKJRepository.findByWorkOrderNum(workNum);
            if (null != lingyongItem) {
                lingyongItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;

        case DCAMConsts.PROCESSS_DZ_LINGYONG_KJ_KEY:

            break;

        case DCAMConsts.PROCESSS_BAOFEI_SL_KEY:
            final List<BaofeiItemSL> baofeiSLItem = this.baofeiItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != baofeiSLItem) {
                baofeiSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_TUIKU_SL_KEY:
            final List<TuikuItemSL> tuikuSLItem = this.tuikuItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != tuikuSLItem) {
                tuikuSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_WEIXIU_SL_KEY:
            final List<WeixiuItemSL> weixiuSLItem = this.weixiuItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != weixiuSLItem) {
                weixiuSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_ZHUANYI_SL_KEY:
            final List<ZhuanyiItemSL> zhuanyiSLItem = this.zhuanyiItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != zhuanyiSLItem) {
                zhuanyiSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_DIAOBO_SL_KEY:
            final List<DiaoboItemSL> diaoboSLItem = this.diaoboItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != diaoboSLItem) {
                diaoboSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_PEIFA_SL_KEY:
            final List<PeifaItemSL> peifaSLItem = this.peifaItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != peifaSLItem) {
                peifaSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_RUKU_SL_KEY:

            break;
        case DCAMConsts.PROCESSS_PEIFA_XH_SL_KEY:

            break;
        case DCAMConsts.PROCESSS_LINGYONG_SL_KEY:
            final List<LingyongConfrimSL> lingyongSLItem = this.lingyongConfrimSLRepository.findByWorkOrderNum(workNum);
            if (null != lingyongSLItem) {
                lingyongSLItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;

        case DCAMConsts.PROCESSS_LINGYONG_XH_SL_KEY:

            break;

        case DCAMConsts.PROCESSS_JIEYONG_SL_KEY:
            final List<JieyongConfrimSL> jieyongItem = this.jieyongConfrimSLRepository.findByWorkOrderNum(workNum);
            if (null != jieyongItem) {
                jieyongItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_GUIHUAN_SL_KEY:
            final List<GuihuanItemSL> guihuanItem = this.guihuanItemSLRepository.findByMain_WorkOrderNum(workNum);
            if (null != guihuanItem) {
                guihuanItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        case DCAMConsts.PROCESSS_RUKU_XH_SL_KEY:

            break;
        case DCAMConsts.PROCESSS_GENGHUAN_KJ_KEY:
            final List<GenghuanItemKJ> genghuanItem = this.genghuanItemKJRepository.findByMain_WorkOrderNum(workNum);
            if (null != genghuanItem) {
                genghuanItem.stream().map(l -> {
                    final Asset asset = this.assetRepository.findByCode(l.getCode());
                    asset.setStatus(l.getStatus());
                    asset.setLocked(false);
                    return asset;
                }).distinct().forEach(list::add);
            }
            break;
        default:
            break;
        }

        if (!list.isEmpty()) {
            this.assetRepository.saveAll(list);
        }
    }

}
